/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-9
 */
package com.flyee.controller.timer{
	
	import com.flyee.controller.tools.cartoon.model.AppearanceConfig;
	import com.flyee.model.Charactor;
	import com.flyee.viewer.CharactorViewer;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * 人物动作动画播放时间对象 
	 */
	public class CartoonTimer extends Timer{
		
		private var _autoPlay:Boolean = false;		
		private var action:int = 0;
		private var direct:int = 0;
		private var frame:int = 0;
		
		private var lastAction : int;
		
		public var char : Charactor;
		public var charUI : CharactorViewer;
		
		public function CartoonTimer(ch : Charactor, ui : CharactorViewer, delay:Number, repeatCount:int=0){
			super(delay, repeatCount);
			char = ch;
			charUI = ui;
			addEventListener(TimerEvent.TIMER, timerHandle);
		}
		
		public override function reset() : void {
			super.reset();
			autoPlay = false;
			action = 0;
			direct = 0;
			frame = 0;
			lastAction = 0;
		}
		
		public function set autoPlay(b:Boolean):void{
			this._autoPlay = b;
			if(this._autoPlay){
				this.reset();
				this.start()
			}else{
				this.stop();
			}
		}
		
		public function get autoPlay():Boolean{
			return this._autoPlay;
		}
		
		public function getAction():int{
			return this.action;
		}
		
		public function getDirect():int{
			return this.direct;
		}
		
		public function getFrame():int{
			return this.frame;
		}
		
		public function setCurrentStatus(act : int, dir : int = -1, frm : int = -1) : void {
			if(act!=1){
				this.lastAction = this.action;
				this.frame = 0;
			}
			this.action = act;
			this.direct = dir;
			this.frame = frm;	
			if(charUI.contains(charUI.charactorContainer) && charUI.contains(charUI.weaponContainer)){		
				var a : Boolean;
				var b : Boolean;
				if(action==AppearanceConfig.MOVE_BY_WALK || action==AppearanceConfig.MOVE_BY_MAGICE || action==AppearanceConfig.MOVE_BY_STAND)
					a = true;
				else
					a = false;
				if(direct>1 && direct<6)
					b = true;
				else
					b = false;
				if(a==b){
					if(charUI.getChildIndex(charUI.charactorContainer)<charUI.getChildIndex(charUI.weaponContainer))
						charUI.swapChildren(charUI.charactorContainer, charUI.weaponContainer);
				}else{
					if(charUI.getChildIndex(charUI.charactorContainer)>charUI.getChildIndex(charUI.weaponContainer))
						charUI.swapChildren(charUI.charactorContainer, charUI.weaponContainer);
				}
					
			}
		} 
		
		public function timerHandle(e:TimerEvent = null) : void {
			while(charUI.charactorContainer.numChildren > 0){
				charUI.charactorContainer.removeChildAt(0);
			}			

			if(char.moveStatus == false && char.skillStatus == false){
				if(action == AppearanceConfig.MOVE_BY_READY){
				
				}else{
					action = AppearanceConfig.MOVE_BY_STAND;
				}
			}
	
			if(charUI.plateArray[action] == null){
				if(charUI.plateArray[AppearanceConfig.MOVE_BY_STAND]!=null)
					action = AppearanceConfig.MOVE_BY_STAND;
				else
					return;
			}
			direct = direct % charUI.plateArray[action].length;
			if(charUI.plateArray[action][direct] == null){
				return;
			}
			frame = (frame) % charUI.plateArray[action][direct].length;
			if(charUI.plateArray[action] == null){
				charUI.charactorContainer.addChild(charUI.plateArray[AppearanceConfig.MOVE_BY_STAND][direct][0]);	
				if(charUI.weaponCartoon != null)	
					charUI.weaponCartoon.gotoAndStop("w" + AppearanceConfig.MOVE_BY_STAND + direct + 0);
			}else{
				charUI.charactorContainer.addChild(charUI.plateArray[action][direct][frame]);	
				if(charUI.weaponCartoon != null)	
					charUI.weaponCartoon.gotoAndStop("w" + action + direct + frame);			
			}
			frame++;
			if(action == AppearanceConfig.MOVE_BY_FIGHT){
				if(frame == charUI.plateArray[action][direct].length){
//					char.skillStatus = false;
					action = AppearanceConfig.MOVE_BY_READY;
				}
			}
		}
		
		public function dispose() : void {
			this.stop();
			this.char = null;
			this.charUI = null;
			this.removeEventListener(TimerEvent.TIMER, timerHandle);
		}
		 
	}
}